package com.fruit.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GoodsUtil {

	public static List<Map<String, Object>> getGoodsPriceList(List<Map<String, Object>> prl, int minShowNum){
		for(int i = 0; i < prl.size(); i++){
			Map<String, Object> goodsRange = prl.get(i);
			Integer maxNum = (Integer) goodsRange.get("maxNum");
			maxNum = (maxNum == -1)?minShowNum+1:maxNum;
				if (minShowNum > maxNum) {
					prl.remove(i);
				}else{
					goodsRange.put("minNum", minShowNum);
				}		

		}
		return prl;
	}
	
	public static void addGoodsPriceList(List<Map<String, Object>> newPrl, BigDecimal minNum, BigDecimal maxNum, BigDecimal price){
		Map<String, Object> goodsPrice = new HashMap<String, Object>();
		goodsPrice.put("minNum", minNum);
		goodsPrice.put("maxNum", maxNum);
		goodsPrice.put("price", price);
		newPrl.add(goodsPrice);
	}
	
	public static List<Map<String, Object>> getGoodsPriceList(List<Map<String, Object>> prl, BigDecimal ratio){
		List<Map<String, Object>> newPrl = new ArrayList<Map<String, Object>>();
		
		BigDecimal curMinNum = BigDecimal.ZERO;
		BigDecimal curMaxNum = BigDecimal.ZERO;
		BigDecimal curPrice = BigDecimal.ZERO;
		BigDecimal max = new BigDecimal("-1");
		for(int i = 0; i < prl.size(); i++){
			Map<String, Object> goodsRange = prl.get(i);
			BigDecimal maxNum = DataUtil.getBigDecimal(goodsRange.get("maxNum"));
			BigDecimal minNum = DataUtil.getBigDecimal(goodsRange.get("minNum"));
			BigDecimal price = DataUtil.getBigDecimal(goodsRange.get("price"));
			if(ratio.compareTo(maxNum) > 0 && maxNum.compareTo(max) != 0){
				continue;
			}
			BigDecimal newMinNum = minNum.divide(ratio,0,BigDecimal.ROUND_UP);
			newMinNum = newMinNum.compareTo(BigDecimal.ZERO) == 0?new BigDecimal(1):newMinNum;
			BigDecimal newMaxNum = maxNum.divide(ratio,0,BigDecimal.ROUND_DOWN);
			//第一次
			if(curMinNum.compareTo(BigDecimal.ZERO) == 0){
				curPrice = price;
				curMinNum = newMinNum;
				if(maxNum.compareTo(max) == 0){
					addGoodsPriceList(newPrl,curMinNum,max,curPrice);
					break;
				}
				
				if(newMinNum.compareTo(newMaxNum) == 1){
					curMaxNum = newMinNum;	
				}else{
					curMaxNum = newMaxNum;
				}
				continue;
			}else{
				//区间入库
				if(newMinNum.compareTo(curMaxNum) == 1){
					addGoodsPriceList(newPrl,curMinNum,curMaxNum,curPrice);
					curPrice = price;
					curMinNum = newMinNum;
					if(maxNum.compareTo(max) == 0){
						addGoodsPriceList(newPrl,curMinNum,max,curPrice);
						break;
					}
					
					if(newMinNum.compareTo(newMaxNum) == 1){
						curMaxNum = newMinNum;	
					}else{
						curMaxNum = newMaxNum;
					}
					continue;
				}else{
					if(maxNum.compareTo(max) == 0){
						addGoodsPriceList(newPrl,curMinNum,max,price);
						break;
					}
					
					if(newMaxNum.compareTo(newMinNum) == 1){
						addGoodsPriceList(newPrl,curMinNum,curMaxNum,curPrice);
						curPrice = price;
						curMinNum = curMaxNum.add(new BigDecimal(1));
						curMaxNum = newMaxNum;
					}
					continue;
				}
			}
			
		}
		return newPrl;
	}
	
	public static void main(String args[]){
		Map<String, Object> goodsPrice1 = new HashMap<String, Object>();
		goodsPrice1.put("minNum", 1);
		goodsPrice1.put("maxNum", 1000);
		goodsPrice1.put("price", 100);
		
		Map<String, Object> goodsPrice2 = new HashMap<String, Object>();
		goodsPrice2.put("minNum", 1001);
		goodsPrice2.put("maxNum", 1200);
		goodsPrice2.put("price", 99);
		
		Map<String, Object> goodsPrice3 = new HashMap<String, Object>();
		goodsPrice3.put("minNum", 1201);
		goodsPrice3.put("maxNum", 1300);
		goodsPrice3.put("price", 98);
		
		Map<String, Object> goodsPrice4 = new HashMap<String, Object>();
		goodsPrice4.put("minNum", 1301);
		goodsPrice4.put("maxNum", 1400);
		goodsPrice4.put("price", 97);
		
		Map<String, Object> goodsPrice5 = new HashMap<String, Object>();
		goodsPrice5.put("minNum", 1401);
		goodsPrice5.put("maxNum", 1500);
		goodsPrice5.put("price", 96);
		
		Map<String, Object> goodsPrice6 = new HashMap<String, Object>();
		goodsPrice6.put("minNum", 1501);
		goodsPrice6.put("maxNum", 1600);
		goodsPrice6.put("price", 95);
		
		Map<String, Object> goodsPrice7 = new HashMap<String, Object>();
		goodsPrice7.put("minNum", 1601);
		goodsPrice7.put("maxNum", -1);
		goodsPrice7.put("price", 94);
		
		List<Map<String, Object>> prl = new ArrayList<Map<String, Object>>();
		prl.add(goodsPrice1);
		prl.add(goodsPrice2);
		prl.add(goodsPrice3);
		prl.add(goodsPrice4);
		prl.add(goodsPrice5);
		prl.add(goodsPrice6);
		prl.add(goodsPrice7);
		
	System.out.println(getGoodsPriceList(prl,new BigDecimal("50")));
		
	}
}
